Skip to content

fix(adapters): use latest onUnmount callback during cleanup#194

Open
gwagjiug wants to merge 3 commits intoTanStack:mainfrom
gwagjiug:fix-latest-onunmount-cleanup
Open

fix(adapters): use latest onUnmount callback during cleanup#194
gwagjiug wants to merge 3 commits intoTanStack:mainfrom
gwagjiug:fix-latest-onunmount-cleanup

Conversation

@gwagjiug
Copy link
Copy Markdown

@gwagjiug gwagjiug commented Apr 11, 2026

🎯 Changes

This PR fixes a stale cleanup callback issue in adapter hooks that support onUnmount.

Before this change, these hooks registered an unmount cleanup with an empty dependency array and read mergedOptions.onUnmount directly inside that cleanup. That meant the cleanup closed over the value from the first render. If a consumer re-rendered with a new onUnmount callback, the hook could still call the old callback during unmount.

This change keeps the existing "register cleanup once" behavior, but makes the cleanup read the latest onUnmount callback instead of the initial one.

Updated hooks:

  • packages/react-pacer/src/debouncer/useDebouncer.ts
  • packages/react-pacer/src/async-debouncer/useAsyncDebouncer.ts
  • packages/react-pacer/src/rate-limiter/useRateLimiter.ts
  • packages/preact-pacer/src/debouncer/useDebouncer.ts

Why this approach

I wanted to keep this fix as small and low-risk as possible.

I considered whether the previous behavior might have been intentional, since the cleanup effect is deliberately registered with [] to keep teardown stable. However, the rest of these hooks already update runtime behavior from the latest render by reassigning fn and calling setOptions(mergedOptions) on each render. Given that, treating onUnmount as "initial render only" felt inconsistent with the rest of the hook contract and with how consumers would naturally expect a cleanup option to behave.

Because of that, I treated this as a stale-closure bug rather than an intentional "initial-only" API.

What changed in the implementation

The change is intentionally minimal:

  • keep the existing empty-deps cleanup effect
  • keep the existing default cleanup behavior
  • avoid introducing a new custom hook such as useLatest
  • avoid changing the public API
  • store only the latest onUnmount callback in a ref
  • read ref.current inside the unmount cleanup

So the behavior changes only in one place:

  • previously: cleanup used the first-render onUnmount
  • now: cleanup uses the latest onUnmount

Motivation

This matters when onUnmount depends on props or render-time values and changes over the component lifecycle. In that case, the old behavior could execute outdated cleanup logic during unmount.

This PR fixes that without changing how often the effect is registered or how the default teardown works.

Local validation

  • pnpm run test:pr --base=53fb52ec --head=HEAD
    • Successfully ran targets test:eslint, test:sherif, test:knip, test:docs, test:lib, test:types, build for 86 projects and 4 tasks they depend on
  • pnpm run test:ci
    • Successfully ran targets test:eslint, test:sherif, test:knip, test:docs, test:lib, test:types, build for 169 projects (3m)
  • pnpm changeset status --since=53fb52ec --verbose
    • confirmed patch releases for @tanstack/react-pacer and @tanstack/preact-pacer

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Fixed unmount cleanup callback handling to use the most recent callback provided, ensuring proper teardown behavior when component options change during the component lifecycle.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fa54f7d3-49c4-4215-b6e5-5c3069455486

📥 Commits

Reviewing files that changed from the base of the PR and between 53fb52e and c413741.

📒 Files selected for processing (5)
  • .changeset/fair-donuts-chew.md
  • packages/preact-pacer/src/debouncer/useDebouncer.ts
  • packages/react-pacer/src/async-debouncer/useAsyncDebouncer.ts
  • packages/react-pacer/src/debouncer/useDebouncer.ts
  • packages/react-pacer/src/rate-limiter/useRateLimiter.ts

📝 Walkthrough

Walkthrough

A patch release updates React and Preact Pacer packages to fix unmount callback handling. The onUnmount callback invoked during component teardown now references the most recent options rather than a stale closure captured at effect initialization.

Changes

Cohort / File(s) Summary
Changeset Metadata
.changeset/fair-donuts-chew.md
Added patch release entry documenting the unmount callback fix for both react-pacer and preact-pacer packages.
React Pacer Hooks
packages/react-pacer/src/async-debouncer/useAsyncDebouncer.ts, packages/react-pacer/src/debouncer/useDebouncer.ts, packages/react-pacer/src/rate-limiter/useRateLimiter.ts
Implemented useRef to synchronize onUnmountRef.current with the latest mergedOptions.onUnmount each render, ensuring cleanup effects invoke the current callback instead of a stale closure.
Preact Pacer Hooks
packages/preact-pacer/src/debouncer/useDebouncer.ts
Updated to use useRef for tracking the latest onUnmount callback, invoking onUnmountRef.current during unmount cleanup instead of relying on captured closure values.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 When teardown comes to say goodbye,
Fresh callbacks answer, not stale ones—
Refs keep the latest close at hand,
No closures trapped in yesterday's sand,
Cleanup's now clean, just as planned! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: fixing the use of the latest onUnmount callback during cleanup in adapter hooks.
Description check ✅ Passed The description is comprehensive and complete, covering all required sections: detailed explanation of changes, updated hooks list, rationale, implementation details, motivation, and completed checklist items.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant